using UnityEngine;

namespace Render
{
    public static class Textures
    {
        public static Texture2D Create1D(uint[] pixels, int dimensions, bool mipmap, bool linear)
        {
            Color[] colors = new Color[dimensions];
            Color32 color;

            for (int x=0; x<dimensions; x++)
            {
                Colors.UInt32ToColor32(pixels[x], out color);
                colors[x] = color;
            }

            Texture2D texture = new Texture2D(dimensions, 1, TextureFormat.RGBA32, mipmap, linear);
            texture.SetPixels(colors);
            texture.Apply(mipmap);
            return texture;
        }

        public static Texture2D Create1D(uint[] pixels, int dimensions, bool mipmap, bool linear, int anisoLevel, FilterMode filterMode, TextureWrapMode wrapMode)
        {
            Texture2D texture = Create1D(pixels, dimensions, mipmap, linear);
            texture.anisoLevel = anisoLevel;
            texture.filterMode = filterMode;
            texture.wrapMode = wrapMode;
            return texture;
        }

        public static Texture2D Create2D(uint[] pixels, int dimensions, bool mipmap, bool linear)
        {
            Color[] colors = new Color[dimensions*dimensions];
            Color32 color;
            int index;

            for (int y=0; y<dimensions; y++)
            {
                for (int x=0; x<dimensions; x++)
                {
                    index = (y*dimensions)+x;
                    Colors.UInt32ToColor32(pixels[index], out color);
                    colors[index] = color;
                }
            }

            Texture2D texture = new Texture2D(dimensions, dimensions, TextureFormat.RGBA32, mipmap, linear);
            texture.SetPixels(colors);
            texture.Apply(mipmap);
            return texture;
        }

        public static Texture2D Create2D(uint[] pixels, int dimensions, bool mipmap, bool linear, int anisoLevel, FilterMode filterMode, TextureWrapMode wrapMode)
        {
            Texture2D texture = Create2D(pixels, dimensions, mipmap, linear);
            texture.anisoLevel = anisoLevel;
            texture.filterMode = filterMode;
            texture.wrapMode = wrapMode;
            return texture;
        }
    }
}